﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TheBox
{
    class Box
    {
        public static int BoxHeight = 260;
        public static int BoxWidth = 260;
        public static int Margin = 5;
        public float Rotation = 0.0f;
        public float RotationSpeed = 0.8f;
        public static Vector2 Origin = new Vector2(BoxWidth / 2, BoxHeight / 2);
        public Vector2 Position = new Vector2();
        public bool InRotation = false;
        public Texture2D Texture;
        public Rectangle Rect;
        private int direction;
        private float rotationTarget = 0.0f;
        public Vector2 Gravity = new Vector2(0, 20);
        public List<Particle> Particles = new List<Particle>();
        public List<Fire> Shots = new List<Fire>();
        public int Quater = 0;
        public static int ParticleLeftMost = Particle.Width / 2 + Margin;
        public static int ParticleUpMost = Particle.Height / 2 + Margin;
        public static int ParticleRightMost = BoxWidth - Margin - Particle.Width / 2;
        public static int ParticleDownMost = BoxHeight - Margin - Particle.Height / 2;

        public static int PlayerLeftMost = Player.Width / 2 + Margin;
        public static int PlayerUpMost = Player.Height / 2 + Margin;
        public static int PlayerRightMost = BoxWidth - Margin - Player.Width / 2;
        public static int PlayerDownMost = BoxHeight - Margin - Player.Height / 2;
        
        public Player Player;
        private Random Rand;

        public Box(Random Rand)
        {
            this.Rand = Rand;
        }

        public void StartRotation(float rotation, bool clockWise)
        {
            InRotation = true;
            if (clockWise)
                direction = 1;
            else direction = -1;
            rotationTarget += rotation * direction;
            Quater += direction;
            Quater %= 4;
            if (Quater < 0)
                Quater = 4 + Quater;
        }

        public void update(GameTime gameTime)
        {
            if (InRotation)
            {
                Rotation += (float)gameTime.ElapsedGameTime.TotalSeconds * RotationSpeed * direction;
                if (direction * (Rotation - rotationTarget) > 0)
                {
                    InRotation = false;
                    Rotation = rotationTarget;
                }
            }

            Fire[] tempShotsList = Shots.ToArray();
            foreach (Fire curFire in tempShotsList)
            {
                curFire.update(gameTime);
                Vector2 realPos = Position - Origin;
                if (!curFire.Rect.Intersects(new Rectangle((int)realPos.X, (int)realPos.Y, BoxWidth, BoxHeight)))
                    Shots.Remove(curFire);
            }

            Particle[] tempParticleList = Particles.ToArray();
            foreach (Particle p in tempParticleList)
            {

                bool shotHit = false;
                foreach (Fire curFire in Shots)
                {
                    if (p.Rect.Intersects(curFire.Rect))
                    {
                        Particles.Remove(p);
                        shotHit = true;
                        curFire.MyPlayer.AddScore();
                        break;
                    }
                }
                if (shotHit)
                    continue;

                if (p.Rect.Intersects(Player.Rect))
                {
                    Particles.Remove(p);
                    Player.Hit();
                    continue;
                }

                foreach (Particle p2 in Particles)
                {
                    if( (p != p2) && (p.Rect.Intersects(p2.Rect)) )
                    {
                        int intersectCount = 0;
                        while (p.Rect.Intersects(p2.Rect) && (intersectCount <= 10))
                        {
                            intersectCount++;
                            p.Pos -= 4 * p.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                        }
                        if (intersectCount > 10)
                        {
                            Particles.Add(new Particle(this,p.Texture,Rand));
                            Particles.Remove(p);
                            break;
                        }
                        else
                        {
                            p.Speed *= -0.5f;
                        }
                    }
                }



                p.update(gameTime);
                if ((p.Pos.X < ParticleLeftMost) || (p.Pos.Y < ParticleUpMost) ||
                    (p.Pos.X > ParticleRightMost) || (p.Pos.Y > ParticleDownMost))
                {
                    p.Speed = new Vector2(0, 0);
                    if (p.Pos.X < ParticleLeftMost)
                        p.Pos.X = ParticleLeftMost;
                    if (p.Pos.Y < ParticleUpMost)
                        p.Pos.Y = ParticleUpMost;
                    if (p.Pos.X > ParticleDownMost)
                        p.Pos.X = ParticleDownMost;
                    if (p.Pos.Y > ParticleRightMost)
                        p.Pos.Y = ParticleRightMost;
                }
            }
            Player.update(gameTime);
            if ((Player.Pos.X < PlayerLeftMost) || (Player.Pos.Y < PlayerUpMost) ||
                (Player.Pos.X > PlayerRightMost) || (Player.Pos.Y > PlayerDownMost))
            {
                Player.Speed = new Vector2(0, 0);
                if (Player.Pos.X < PlayerLeftMost)
                    Player.Pos.X = PlayerLeftMost;
                if (Player.Pos.Y < PlayerUpMost)
                    Player.Pos.Y = PlayerUpMost;
                if (Player.Pos.X > PlayerDownMost)
                    Player.Pos.X = PlayerDownMost;
                if (Player.Pos.Y > PlayerRightMost)
                    Player.Pos.Y = PlayerRightMost;
            }
        }


    }
}
